'''
Created on Dec 5, 2010

@author: jackreilly
'''
from google.appengine.ext import webapp
from jettube.models import Song, Station
from jettube.client.responseUtil import dumpDictionary
from jettube.server.utilities import sendTextToJackFromSong
from django.utils.simplejson import dumps
from jettube.server.utilities.lastfm import getRelatedVideos



class GetSongsHandler(webapp.RequestHandler):
    def get(self):
        dumpDictionary(Song.getSongsDictionaryFromRequest(self.request), self.response)
        
        
class NewSongHandler(webapp.RequestHandler):
    def get(self):
        request = self.request
        station = Station.getStationFromRequest(request).name
        status = Song.verifySongWithYoutubeFromRequest(request)
        if status is Song.VERIFY_OK:
            song = Song.putNewSongFromRequest(request)
            sendTextToJackFromSong(song)
        self.response.out.write(dumps({'station': station,
                                 'status': status,
                                 'messageType': 'addSong'}) )
        
class SimilarSongsHandler(webapp.RequestHandler):
    def get(self):
        request = self.request
        song = Song.getSongFromRequest(request)
        relatedSongs = song.getRelatedSongsDict()
        if not relatedSongs:
            relatedSongs = getRelatedVideos(song.title)
            rsKeys = []
            for rs in relatedSongs:
                rsKeys.append(Song.putRelatedSong(rs['id'], rs['title'], rs['thumb']))
            song.relatedSongs = rsKeys
            song.put()
        dumpDictionary(relatedSongs, self.response)

class AddRecommendedSong(webapp.RequestHandler):
    def get(self):
        request = self.request
        songTitle = request.get('title')
        songID = request.get('vidID')
        